package com.rw.coco.handler.message.reply.handler.command.sender;

import com.rw.coco.api.data.model.CharacterPropertyModel;
import com.rw.coco.expression.DiceExpression;
import com.rw.coco.expression.parser.DiceExpressionParser;
import com.rw.coco.handler.message.event.CocoMessageEvent;
import com.rw.coco.handler.message.reply.handler.command.assistant.ra.SuccessLevel;
import com.rw.coco.handler.message.reply.handler.command.exception.ra.PropNotFoundException;
import com.rw.coco.service.CharacterPropertyService;
import com.rw.coco.util.DiceRule;
import com.rw.tool.util.random.RandomUtil;

import java.util.Optional;

public class SCSender extends BaseSender {
    // 检定成功表达式
    private final String successExpression;

    // 鉴定失败表达式
    private final String failExpression;

    public SCSender(String successExpression, String failExpression, CocoMessageEvent event) {
        super(event);
        this.successExpression = successExpression;
        this.failExpression = failExpression;
    }


    /**
     * 进行san值鉴定
     */
    public void check() {
        int san = getSan();
        int randomNum = RandomUtil.nextInt(100) + 1;

        // 没有查到sc使用的是什么大成功判断规则，所以直接使用标准规则
        SuccessLevel successLevel = DiceRule.STANDARD_RULE.roll(san, randomNum);
        if (successLevel.equals(SuccessLevel.FATAL)) {
            // 如果大失败
            fatal(san, randomNum);
        } else if (successLevel.equals(SuccessLevel.CRITICAL_SUCCESS)) {
            // 如果大成功
            fatal(san, randomNum);
        } else if (successLevel.equals(SuccessLevel.FAILURE)) {
            // 如果失败
            fail(san, randomNum);
        } else {
            // 如果成功
            success(san, randomNum);
        }
    }

    private int getSan() {
        CharacterPropertyService service = new CharacterPropertyService(mapper);
        Optional<CharacterPropertyModel> sanOptional = service.getProp(accountModel, mainPropName.san);

        // 如果没有找到san值，则直接抛出异常
        if (!sanOptional.isPresent()) {
            throw new PropNotFoundException(mainPropName.san);
        }

        return sanOptional.get().getPropertiesValue();
    }

    private void fatal(int san, int randomNum) {

    }

    private void criticalSuccess(int san, int randomNum) {

    }

    private void fail(int san, int randomNum) {
        // 如果失败
        DiceExpressionParser parser = new DiceExpressionParser(accountModel.getDefaultDice());
        DiceExpression diceExpression = parser.parseDiceExpression(failExpression);

        // 计算出减少的量
        int minus = diceExpression.calculation().intValue();

        // 保存结果
        CharacterPropertyService service = new CharacterPropertyService(mapper);
        service.addPropValue(accountModel, mainPropName.san, -minus);

        // 回复
        event.sendBack(diceReply.scFail(event, diceExpression, san, minus, randomNum));
    }

    private void success(int san, int randomNum) {
        // 如果成功
        DiceExpressionParser parser = new DiceExpressionParser(accountModel.getDefaultDice());
        DiceExpression diceExpression = parser.parseDiceExpression(successExpression);

        // 计算出减少的量
        int minus = diceExpression.calculation().intValue();

        // 保存结果
        CharacterPropertyService service = new CharacterPropertyService(mapper);
        service.addPropValue(accountModel, mainPropName.san, -minus);

        // 回复
        event.sendBack(diceReply.scSuccess(event, diceExpression, san, minus, randomNum));
    }
}
